-- 巫妖雕像
require "game/ui/form/blacksmith/UIWitchStatue"

function initWitchStatue(self, buildingInfo, dbase, pos)
    if type(dbase) ~= "table" or type(dbase["need_property"]) ~= "number" then
        -- 没要求，配置错误
        error("获取不到巫妖雕像物品配置！");
        return;
    end

    -- 雇佣巫妖王
    local function okFun()
        -- 尝试尝试触发
        DungeonActionM.go("slime_event", pos, 109);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    -- 获得碎片
    local function okFun2()
        -- 尝试尝试触发
        DungeonActionM.go("slime_event", pos, 110);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    local petId = SlimeEventM.query(109, "dbase")["pet_id"];
    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 格子失效了
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 雕像等级
            local statueInfo = DemonStatueM.getDemonInfo("demigod_lich");
            local level = statueInfo["cur_level"] or 0;

            local needLevel = FormulaM.invoke("CALC_WITCH_STATUE_NEED_LEVEL");
            if level < needLevel then
                -- 没有达到等级
                self:openBuildingView(nil, 0, buildingInfo["dialog2"], buildingInfo["view_text2"], nil, nil, nil, true);
            else
                if PetM.getMyPet(petId) then
                    -- 有巫妖王冈布奥
                    local uiWitchStatue = UIWitchStatue.create(self.gridData, okFun2);
                    UIMgr.getCurrentScene():addForm(uiWitchStatue);
                else
                    -- 没有巫妖王冈布奥
                    local uiWitchStatue = UIWitchStatue.create(self.gridData, okFun);
                    UIMgr.getCurrentScene():addForm(uiWitchStatue);
                end
            end
            AudioM.playFx("workshop_store");
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:addTouchEventListener(onClicked);
end
